﻿
using BookStore.Solution.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BookStore.Solution.BLL
{
   public class BillManager
    {
        private DataTable CreateTable(List<Bill> list)
        {
            DataTable tb = new DataTable();
            tb.Columns.Add("BillID", Type.GetType("System.Int32"));
            tb.Columns.Add("EmployeeID", Type.GetType("System.String"));
            tb.Columns.Add("Date", Type.GetType("System.DateTime"));
            tb.Columns.Add("PromotionID", Type.GetType("System.String"));
            tb.Columns.Add("Status", Type.GetType("System.String"));
            foreach (Bill a in list)
            {
                DataRow newrow = tb.NewRow();
                object[] array = new object[5];
                array[0] = a.BillID;
                array[1] = a.EmployeeID;
                array[2] = a.Date;
                array[3] = a.PromotionID;
                array[4] = a.Status;
                newrow.ItemArray = array;
                tb.Rows.Add(newrow);
            }
            return tb;
        }
        public DataTable GetAll()
        {
            List<Bill> BillList=null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
             BillList = (from a in ctx.Bills where a.Status.CompareTo("Đang tồn tại")==0 select a).ToList<Bill>();
            }
            return CreateTable(BillList);
        }
        public DataTable SearchID(int ID)
        {
            List<Bill> bills = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                bills = ctx.Bills.Where(a => a.BillID == ID).ToList<Bill>();
            }
            if(bills.Count!=0)
            return CreateTable(bills);
            return null;
        }
        public DataTable SearchByDay(DateTime day)
        {
            List<Bill> bills = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                bills = ctx.Bills.Where(a => a.Date.Day == day.Day && a.Date.Month == day.Month && a.Date.Year == day.Year).ToList<Bill>();
            }
            if (bills.Count != 0)

                return CreateTable(bills);
            return null;
        }
        public DataTable SearchByDay(DateTime Startday,string Type)
        {
            List<Bill> bills = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                switch (Type)
                {
                    case "Month":
                        bills = ctx.Bills.Where(a => a.Date.Month == Startday.Date.Month && a.Status.CompareTo("Đang tồn tại") == 0 && a.Date.Year == Startday.Date.Year).ToList<Bill>();
                        break;
                    case"Year":
                        bills = ctx.Bills.Where(a => a.Date.Year == Startday.Date.Year && a.Status.CompareTo("Đang tồn tại") == 0).ToList<Bill>();
                        break;
                }
                
            }
            if (bills.Count != 0)
            return CreateTable(bills);
            return null;
        }
        public int Insert(Bill b)
        {
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                ctx.Bills.Add(b);
                if (ctx.SaveChanges() > 0)
                {
                    SystemLogManager.Insert("Thêm hóa đơn: " + b);
                    return b.BillID;
                }
                else
                    return 0;
            }
        }
        public String Delete(Bill bill)
        {
            Bill b = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                b = ctx.Bills.Where(a => a.BillID == bill.BillID).FirstOrDefault<Bill>();
                if (b != null)
                {
                    b.Status = bill.Status;
                    ctx.SaveChanges();
                    SystemLogManager.Insert("Xóa hóa đơn: " + b);
                    return "Xóa thành công";
                }
                else
                    return "Hóa đơn không tồn tại";
            }
        }
        public string UpdateStatus(Bill t)
        {
            Bill bill = null;
            string kq="";
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                
                bill = ctx.Bills.Where(a => a.BillID == t.BillID).FirstOrDefault<Bill>();
                try
                {
                    if (bill != null)
                    {
                        bill.Status = t.Status;
                        bill.Date = t.Date;
                        bill.PromotionID = t.PromotionID;
                        ctx.SaveChanges();
                        SystemLogManager.Insert("Cập nhật hóa đơn: " + t);
                        t = bill;
                        kq= "Cập nhật thành công";
                    }
                }
                catch
                {
                    kq= "Cập nhật thất bại";
                }
            }
            return kq;
        }
        public string GetNameProduct(string productID)
        {
            Product product = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                product = (from a in ctx.Products where a.ProductID.CompareTo(productID) == 0  select a).FirstOrDefault<Product>();
            }
            return product.ProductName;
        }
    }
}
